package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.permissionsystem1_1.businessFunction.dao.entity.po.DepartmentInfoPo;
import com.example.permissionsystem1_1.businessFunction.dao.entity.po.DepartmentUserPo;
import com.example.permissionsystem1_1.businessFunction.dao.repository.DepartmentInfoDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.DepartmentUserDao;
import com.example.permissionsystem1_1.businessFunction.service.converter.DepartmentInfoServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.departmentInfoReqModel.*;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.departmentInfoRspModel.*;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.DepartmentInfoService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentInfoServiceImpl extends BaseServiceImpl implements DepartmentInfoService {
    @Autowired
    private DepartmentInfoDao departmentInfoDao;
    @Autowired
    private DepartmentUserDao departmentUserDao;

    @Override
    public DepartmentInfoInsertRspModel insert(DepartmentInfoInsertReqModel reqModel) {
        DepartmentInfoPo po= DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        //字段重复性校验:如果重复,则会将po变成空,从而阻止接下去的数据库操作
        List<String> fieldNames=new ArrayList<>();
        fieldNames.add("departmentName");
        fieldNames.add("departmentCode");
        try {
            po=fieldsRepeatCheck(departmentInfoDao,"selects",po,fieldNames,"insert");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            po=null;
        }
        if(po!=null){
            resultInt=departmentInfoDao.insert(po);
        }
        return DepartmentInfoServiceConverter.covertToRspModel(resultInt);
    }

//    @Override
//    public TestInsertBatchRspModel insertBatch(TestInsertBatchReqModel reqModel) {
//        List<TestPo> pos=TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(pos!=null&&pos.size()>0){
//            resultSum=testDao.insertBatch(pos);
//        }
//        return TestServiceConverter.covertToInsertBatchRspModel(resultSum);
//    }

    @Override
    @Transactional
    public DepartmentInfoDeleteByIdRspModel deleteById(DepartmentInfoDeleteByIdReqModel reqModel) {
        DepartmentInfoPo po= DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        //关联数据清理:根据部门id删除当前部门下的用户的关联记录
        deleteDepartmentUserByDepartmentId(po.getId());
        return DepartmentInfoServiceConverter.covertToDeleteByIdRspModel(departmentInfoDao.deleteById(po.getId()));
    }

//    @Override
//    public TestDeletesRspModel deletes(TestDeletesReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null){
//            resultSum=testDao.deletes(po);
//        }
//        return TestServiceConverter.covertToDeletesRspModel(resultSum);
//    }

//    @Override
//    public TestDeleteBatchByIdsRspModel deleteBatchByIds(TestDeleteBatchByIdsReqModel reqModel) {
//        TestPo po=TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
//            resultSum=testDao.deleteBatchByIds(po.getIds());
//        }
//        return TestServiceConverter.covertToDeleteBatchByIdsRspModel(resultSum);
//    }

    @Override
    public DepartmentInfoUpdateByIdRspModel updateById(DepartmentInfoUpdateByIdReqModel reqModel) {
        DepartmentInfoPo po= DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        //字段重复性校验:如果重复,则会将po变成空,从而阻止接下去的数据库操作
        List<String> fieldNames=new ArrayList<>();
        fieldNames.add("departmentName");
        fieldNames.add("departmentCode");
        try {
            po=fieldsRepeatCheck(departmentInfoDao,"selects",po,fieldNames,"update");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            po=null;
        }
        if(po!=null){
            resultInt=departmentInfoDao.updateById(po);
        }
        return DepartmentInfoServiceConverter.covertToUpdateByIdRspModel(resultInt);
    }

//    @Override
//    public TestUpdatesRspModel updates(TestUpdatesReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getByPo()!=null&&po.getSetPo()!=null){
//            resultSum=testDao.updates(po.getByPo(),po.getSetPo());
//        }
//        return TestServiceConverter.covertToUpdatesRspModel(resultSum);
//    }

//    @Override
//    public TestUpdateBatchByIdsRspModel updateBatchByIds(TestUpdateBatchByIdsReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
//            resultSum=testDao.updateBatchByIds(po);
//        }
//        return TestServiceConverter.covertToUpdateBatchByIdsRspModel(resultSum);
//    }

    @Override
    public DepartmentInfoSelectByIdRspModel selectById(DepartmentInfoSelectByIdReqModel reqModel) {
        DepartmentInfoPo po= DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        if(po!=null&&po.getId()!=null){
            po=departmentInfoDao.selectById(po.getId());
        }
        return DepartmentInfoServiceConverter.covertToSelectByIdRspModel(po);
    }

    @Override
    public DepartmentInfoSelectsRspModel selects(DepartmentInfoSelectsReqModel reqModel) {
        DepartmentInfoPo po= DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        List<DepartmentInfoPo> pos=new ArrayList<>();
        if(po!=null){
            pos=departmentInfoDao.selects(po);
        }
        return DepartmentInfoServiceConverter.covertToSelectsModel(pos);
    }

//    @Override
//    public TestSelectBatchByIdsRspModel selectBatchByIds(TestSelectBatchByIdsReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        List<TestPo> pos=new ArrayList<>();
//        if(po!=null){
//            pos=testDao.selectBatchByIds(po.getIds());
//        }
//        return TestServiceConverter.covertToSelectBatchByIdsModel(pos);
//    }

    @Override
    public DepartmentInfoSelectPageRspModel selectPage(DepartmentInfoSelectPageReqModel reqModel) {
        DepartmentInfoPo po=DepartmentInfoServiceConverter.convertReqModelToPo(reqModel);
        if(po==null){
            return null;
        }
        Page page=new Page(reqModel.getPageNo(),reqModel.getPageSize());
        //默认按照年龄排序
        page.addOrder(OrderItem.asc("id"));
        IPage<DepartmentInfoPo> iPagePo=departmentInfoDao.selectPage(page,po);
        return DepartmentInfoServiceConverter.covertToSelectPageModel(iPagePo);
    }
    //***********************下面是特定业务功能方法

    /** 关联数据清理:根据部门id删除当前部门下的用户的关联记录 */
    private void deleteDepartmentUserByDepartmentId(Long departmentId){
        DepartmentUserPo departmentUserPo=new DepartmentUserPo();
        departmentUserPo.setDepartmentId(departmentId);
        departmentUserDao.deletes(departmentUserPo);
    }

}
